//
//  AppDelegate.m
//  ProjectA
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "AppDelegate.h"


#import "UMSocial.h"
#import "UMSocialSinaSSOHandler.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"

#import "ViewController.h"
#import "ViewController4.h"
#import "ViewController5.h"
#import "SettingsViewController.h"


#import "NetWorkHandler.h"
#import "DataBaseHandler.h"
#import "AppMacro.h"

@interface AppDelegate ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UITabBarController *tb;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *arrForData;
@end

@implementation AppDelegate

- (void)dealloc {
    [_imageView release];
    [_arrForData release];
    [_scrollView release];
    [_window release];
    [_tb release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        //调用其他SDK，例如支付宝SDK等
    }
    return result;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [UMSocialData setAppKey:@"56d10927e0f55a08e7002f24"];
    //打开新浪微博的SSO开关，设置新浪微博回调地址，这里必须要和你在新浪微博后台设置的回调地址一致。
    [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"1300715127"
                                              secret:@"8e7fca6fce4dc00fd95bacb34f44c52b"
                                         RedirectURL:@"https://api.weibo.com/oauth2/default.html"];
    
//    //设置微信AppId、appSecret，分享url
//    [UMSocialWechatHandler setWXAppId:@"wx014ef229f12a7706" appSecret:@"1334a7f601b1f581d23234e687bbdddf" url:@"http://www.umeng.com/social"];
    
    [UMSocialQQHandler setQQWithAppId:@"1105161267" appKey:@"uYzcH0GXK9ovd9xC" url:@"http://www.umeng.com/social"];
    
    
    //UMShareToSina(新浪微博)
    //UMShareToWechatTimeline(微信朋友圈)
    //UMShareToQQ(qq)
    // 如果该设备没有安装该App能隐藏该按钮
    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToSina, UMShareToWechatTimeline, UMShareToQQ]];
   
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    self.window.backgroundColor = [UIColor whiteColor];

    
    [self createSQL];
    
    [self createTabBarController];
    
    
    [self createImage];
    
    
    
    [_window release];
    
    return YES;
}
- (void)createImage {
    //[NSThread sleepForTimeInterval:3.0];
    self.imageView = [[UIImageView alloc] initWithFrame:self.window.bounds];
    
    self.imageView.image = [UIImage imageNamed:@"iconfont-qidongtupian"];
    [self.window addSubview:self.imageView];
    [self.imageView release];
    [self.window bringSubviewToFront:self.imageView];

    [self performSelector:@selector(removeEnterView) withObject:nil afterDelay:2.0f];
}
- (void)removeEnterView {
    [self.imageView removeFromSuperview];
    
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"引导页"]) {
        
        NSLog(@"第一次启动");
        
        [self createScrollViewForYidao];
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"引导页"];
    }
}
- (void)createScrollViewForYidao {
    
    self.arrForData = @[@"1", @"2", @"3", @"4"];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.window.bounds];
    [self.window addSubview:self.scrollView];
    [self.scrollView release];
    
    self.scrollView.contentSize = CGSizeMake(self.window.frame.size.width * self.arrForData.count, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    self.scrollView.backgroundColor = [UIColor redColor];
    for (int i = 0; i < self.arrForData.count; i++) {
        
        NSString *image = [self.arrForData objectAtIndex:i];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]];
        imageView.frame = CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        [self.scrollView addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        [imageView release];
        
        if ((self.arrForData.count - 1) == i) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(120, self.window.frame.size.height - 50 - 20, self.window.frame.size.width - 240, 50);
            [button setBackgroundImage:[UIImage imageNamed:@"buttonPic"] forState:UIControlStateNormal];
            //[button setImage:[UIImage imageNamed:@"buttonPic"] forState:UIControlStateNormal];
            [imageView addSubview:button];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            
        }
    }
}

- (void)buttonAction:(UIButton *)button {
    
    [self.scrollView removeFromSuperview];
    
}

- (void)createSQL {
    
    [[DataBaseHandler shareDataBaseHandler] open];
    [[DataBaseHandler shareDataBaseHandler] createTable];
    
}

- (void)createTabBarController {
    
    
    
    self.tb = [[UITabBarController alloc] init];
    self.window.rootViewController = self.tb;
    [self.tb release];
    
   // self.tb.tabBar.tintColor = [UIColor whiteColor];
    
   // self.tb.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self createVC];

}

- (void)createVC {
    // 创建vc1
    ViewController *vc1 = [[ViewController alloc] init];
    vc1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"推荐" image:[UIImage imageNamed:@"iconfont-wujiaoxing(huise)"] tag:500];
    UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:vc1];
    [vc1 release];
    
    // 创建vc4
    ViewController4 *vc4 = [[ViewController4 alloc] init];
    vc4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"目的地" image:[UIImage imageNamed:@"iconfont-jingse()"] tag:501];
    UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:vc4];
    [vc4 release];
    
    // 创建vc5
    ViewController5 *vc5 = [[ViewController5 alloc] init];
    vc5.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"iconfont-wo"] tag:502];
    UINavigationController *navi5 = [[UINavigationController alloc] initWithRootViewController:vc5];
    [vc5 release];
    
    [self.tb setViewControllers:@[navi1, navi4, navi5]];
    
    [navi1 release];
    [navi4 release];
    [navi5 release];
    
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
